package io.sdph.products;


import io.sdph.products.exceptions.ProductException;
import io.sdph.products.model.Product;
import io.sdph.products.dao.MagazineManagerDAO;
import io.sdph.products.dao.MagazineManagerDB;
import io.sdph.products.dao.ProductsManagerDAO;
import io.sdph.products.dao.ProductsManagerDB;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Manager zarządzający produktami.
 * 
 * @author 
 * @since 0.0.1
 */
@Service
public class ProductManager implements IProductManager {

	@Autowired
	ProductsManagerDAO managerDAO;
	
	@Autowired
	MagazineManagerDAO managerDAO2;

	public void addProduct(Product product) throws ProductException {

		managerDAO.saveProduct(product);


	}

	public void modifyProduct(Product product) throws ProductException {

		managerDAO.saveProduct(product);

	}

	public void deleteProduct(Product product) throws ProductException {

		managerDAO2.removeQuantity(product);
		managerDAO.removeProduct(product);


	}

	public Product getProduct(String id) throws ProductException {

		Product produkt;
		produkt = managerDAO.getProduct(id);
		return produkt;


	}

	public List<Product> getProducts(Long limitFrom, Long limitTo)
			throws ProductException {

		List<Product> produkty = new ArrayList<Product>();
		produkty = managerDAO.getProducts();

		if (limitFrom == null)
			limitFrom = (long) 0;
		int min = (int) (long) limitFrom;
		if (limitTo == null)
			limitTo = (long) produkty.size();
		int max = (int) (long) limitTo;
		List<Product> zwroc = new ArrayList<Product>();
		for (int i = min; i < max; i++) {
			Product a = produkty.get(i);
			zwroc.add(a);
		}
		return zwroc;
	}

	public List<Product> getProductsByName(String name, Long limitFrom,
			Long limitTo) throws ProductException {

		List<Product> produkty = new ArrayList<Product>();
		produkty = managerDAO.getProducts();

		if (limitFrom == null)
			limitFrom = (long) 0;
		int min = (int) (long) limitFrom;
		if (limitTo == null)
			limitTo = (long) produkty.size();
		int max = (int) (long) limitTo;
		List<Product> zwroc = new ArrayList<Product>();
		for (int i = min; i < max; i++) {
			if (produkty.get(i).getId() == name) {
				Product a = produkty.get(i);
				zwroc.add(a);
			}
		}
		return zwroc;
	}

	public List<Product> getProductsByCategory(String categoryName,
			Long limitFrom, Long limitTo) throws ProductException {

		List<Product> produkty = new ArrayList<Product>();
		produkty = managerDAO.getProducts();

		if (limitFrom == null)
			limitFrom = (long) 0;
		int min = (int) (long) limitFrom;
		if (limitTo == null)
			limitTo = (long) produkty.size();
		int max = (int) (long) limitTo;
		List<Product> zwroc = new ArrayList<Product>();
		for (int i = min; i < max; i++) {
			if (produkty.get(i).getCategory() == categoryName) {
				Product a = produkty.get(i);
				zwroc.add(a);
			}
		}
		return zwroc;
	}

}
